﻿using NewRetail.Application.Utility.MemoryCommon;
using System;
using System.Collections.Generic;

namespace NewRetail.Application.Utility.Hubs
{
    #region signalr基础接口
    public interface ISignalrMemoryCacheHelper// : IApplicationService
    {
        void AddConnectForUser(string userId, string connectionId);

        List<string> GetConnectsByUserAsync(string userId);

        /// <summary>
        /// 查询指定用户的连接数
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        long GetConnectsCountByUser(string userId);

        void RemoveConnectForUser(string userId, string connectionId);

        void AddUserForGroup(string group, string connectId, string userId);

        void RemoveConnectFromGroup(string group, string connectId);


        /// <summary>
        /// 清空redis缓存中的用户和组
        /// </summary>
        /// <returns></returns>
        void ClearUserAndGroup();
    }
    #endregion

    #region signalr基础实现
    public class SignalrMemoryCacheHelper : /*ApplicationService,*/ ISignalrMemoryCacheHelper
    {
        public static readonly string PREFIXUSER = "signalr_u_";
        public static readonly string PREFIXGROUP = "signalr_g_";
        private readonly ICacheHelper _cache;
        private readonly Func<string, ICacheHelper> _serviceCacheAccessor;

        public SignalrMemoryCacheHelper(Func<string, ICacheHelper> serviceCacheAccessor)
        {
            _serviceCacheAccessor = serviceCacheAccessor;
            _cache = _serviceCacheAccessor(MemoryRedisKey.MemoryName);
        }

        public void AddConnectForUser(string userId, string connectionId)
        {
            _cache.SetCache($"{PREFIXUSER}{userId}", connectionId);
        }

        public List<string> GetConnectsByUserAsync(string userId)
        {
            var ret = (List<string>)_cache.GetCache($"{PREFIXUSER}{userId}");
            return ret;
        }

        /// <summary>
        /// 查询指定用户的连接数
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public long GetConnectsCountByUser(string userId)
        {
            var ret = _cache.GetCache($"{PREFIXUSER}{userId}");
            if (ret != null)
                return 1;
            else
                return 0;
        }

        public void RemoveConnectForUser(string userId, string connectionId)
        {
            _cache.RemoveCache($"{PREFIXUSER}{userId}");
            
        }

        public void AddUserForGroup(string group, string connectId, string userId)
        {
            _cache.SetCache($"{PREFIXGROUP}{group}", $"{connectId}:{userId}");
        }

        public void RemoveConnectFromGroup(string group, string connectId)
        {
            _cache.RemoveCache($"{PREFIXGROUP}{group}");
        }

        /// <summary>
        /// 清空缓存中的用户和组
        /// </summary>
        /// <returns></returns>
        public void ClearUserAndGroup()
        {
            _cache.RemoveCacheAll();
        }
    }
    #endregion
}
